package com.hpin.mlxteacher.base;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Looper;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.Toast;

import com.hpin.mlxteacher.utils.CommonUtils;
import com.hpin.mlxteacher.utils.Constant;
import com.hpin.mlxteacher.utils.LogUtil;
import com.hpin.mlxteacher.widget.MyRelativeLayout;

/**
 * activity 基类
 * Created by Administrator on 2015/7/14.
 */
public abstract class BaseActivity extends Activity implements View.OnClickListener {

    protected MyApplication mApplication;
    protected SharedPreferences sp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        // 竖屏锁定
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        mApplication = (MyApplication) getApplication();

        sp = getSharedPreferences(Constant.CONFIG, MODE_PRIVATE);

    }

//    private void initSystemBar() {
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//            setTranslucentStatus(true);
//            SystemBarTintManager tintManager = new SystemBarTintManager(this);
//            tintManager.setStatusBarTintEnabled(true);
//            tintManager.setStatusBarTintResource(R.color.actionbar_bg);
//            SystemBarConfig config = tintManager.getConfig();
//            listViewDrawer.setPadding(0, config.getPixelInsetTop(true), 0, config.getPixelInsetBottom());
//        }
//    }

    /**
     * 控件点击事件
     */
    public abstract void widgetClick(View v);

    @Override
    public void onClick(View v) {
        widgetClick(v);
    }

    /**
     * 土司
     *
     * @param text
     */
    public void showToast(final String text) {
        if (Thread.currentThread() == Looper.getMainLooper().getThread()) {
            // 如果在主线程中
            showToash(text);

        } else {
            // 在子线程中
            runOnUiThread(new Runnable() {
                public void run() {
                    showToash(text);
                }
            });

        }
    }

    private void showToash(final String text) {
        try {
            if (!CommonUtils.isNull(text)) {
                Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();
            }

        } catch (Exception e) {
            LogUtil.e("tag", "土司出错!!!");
        }

    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            View v = getCurrentFocus();
            if (isShouldHideInput(v, ev)) {

                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null) {
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                }
            }
            return super.dispatchTouchEvent(ev);
        }
        // 必不可少，否则所有的组件都不会有TouchEvent了
        if (getWindow().superDispatchTouchEvent(ev)) {
            return true;
        }
        return onTouchEvent(ev);
    }

    /**
     * 是否需要隐藏软键盘
     *
     * @param v
     * @param event
     * @return
     */
    private boolean isShouldHideInput(View v, MotionEvent event) {
        if (v != null && (v instanceof EditText)) {

            if (v.getParent() instanceof MyRelativeLayout) {

                View pView = (View) v.getParent();
                int[] pLeftTop = {0, 0};
                pView.getLocationInWindow(pLeftTop);
                int left = pLeftTop[0];
                int top = pLeftTop[1];
                int bottom = top + pView.getHeight();
                int right = left + pView.getWidth();
                if (event.getX() > left && event.getX() < right
                        && event.getY() > top && event.getY() < bottom) {
                    // 点击的是评论框区域，保留点击EditText的事件

                    if (CommonUtils.isNull(((EditText) v).getText().toString().trim())) {
                        return false;
                    } else {
                        return true;
                    }
                } else {
                    return true;
                }

            } else {
                int[] leftTop = {0, 0};
                //获取输入框当前的location位置
                v.getLocationInWindow(leftTop);
                int left = leftTop[0];
                int top = leftTop[1];
                int bottom = top + v.getHeight();
                int right = left + v.getWidth();
                if (event.getX() > left && event.getX() < right
                        && event.getY() > top && event.getY() < bottom) {
                    // 点击的是输入框区域，保留点击EditText的事件
                    return false;
                } else {
                    return true;
                }
            }

        }
        return false;
    }

}
